using System.Security.Claims;

namespace RiceShop.Extensions
{
    public static class ClaimsPrincipalExtensions
    {
        public static long GetUserId(this ClaimsPrincipal user)
        {
            var userIdClaim = user.FindFirst(ClaimTypes.NameIdentifier);
            if (userIdClaim == null)
                throw new UnauthorizedAccessException("用户未登录");

            if (!long.TryParse(userIdClaim.Value, out long userId))
                throw new UnauthorizedAccessException("无效的用户ID");

            return userId;
        }
    }
} 